<?php

namespace App\Services;
use App\Models\Beat;
use App\Models\MusicCreation;
use App\Models\WxUsers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MusicService
{

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 分享记录数据
     * @user IHammer
     */
    public static function MusicShareAddHandle($request,&$errmsg){
        $id = $request->get("id")?:0;
        if(empty($id)){
            return false;
        }
        $beat =MusicCreation::getInfo(['id'=>$id,"status"=>1]);
        if(empty($beat)){
            $errmsg = "没有找到对应数据！";
            return false;
        }
        MusicCreation::where(['id' => $id])->increment('share_num',1);
        return true;
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 创建音乐
     */
    public static function MusicCreateHandle($request, &$errmsg){
        $data = $request->input();
        $music = MusicCreation::create($data);
        if($music){
            return true;
        }else{
            $errmsg = "添加音乐失败！";
            return false;
        }
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 编辑音乐
     */
    public static function MusicEditHandle($request, &$errmsg){
        $inputs = $request->input();
        if(empty($inputs['id'])){
            $errmsg = "id不能为空";
            return false;
        }
        $music = MusicCreation::find($inputs['id']);
        if(count($music)){
            $music->beat_id = $inputs['beat_id'];
            $music->mixture_url = $inputs['mixture_url'];
            $music->lyric_content = $inputs['lyric_content'];
            $music->music_title = $inputs['music_title'];
            $music->music_author = $inputs['music_title'];
            $music->save();
            return true;
        }else{
            $errmsg = "未找到该作品！";
            return false;
        }
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 删除音乐
     */
    public static function MusicDeleteHandle($request, &$errmsg){
        $music = MusicCreation::find($request['id']);
        if(count($music)){
            $music->delete();
            if($music->trashed()){
                return true;
            }else{
                $errmsg = "删除失败！";
                return false;
            }
        }else{
            $errmsg = "未找到该作品！";
            return false;
        }
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 获取单个音乐作品
     */
    public static function MusicInfoHandle($id, &$errmsg){
        $music = MusicCreation::find($id);
        if($music){
            return $music->toArray();
        }else{
            $errmsg = "未找到该作品！";
            return false;
        }
    }

    /**
     * @param $request
     * @param $pageSize
     * @return bool
     * @name 获取音乐作品列表
     */
    public static function MusicListsHandle($where,$pageSize){
        $res = MusicCreation::with(['beat'=>function($query){
            $query->select('id','beat_title','beat_author','beat_url');
        },'user'=>function($query){
            $query->select(['id','nickname','avatarurl']);
        }])->where(function($query) use ($where) {
            if(!empty($where['user_id'])){
                $query->where('user_id',$where['user_id']);
            }
        })->orderBy('created_at','desc')
            ->paginate($pageSize);
        if($res){
            $pageData = $res->toArray();
            foreach($pageData['data'] as $key => $item){
                $pageData['data'][$key]['is_collection'] = CollectionService::isCollection($item['id'],'music');
            }
            return $pageData;
        }
        return true;
    }
}
